package ferp.android.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import ferp.android.managers.DeckManager;
import ferp.core.card.Card;

/**
 * User: igorgok
 * Date: 1/28/14 12:57 PM
 */
public class SuitView extends ImageView implements DeckManager.RefreshableView
{
  public SuitView(Context context, float scale) {this(context, null, 0, scale);}
  public SuitView(Context context, AttributeSet attrs) {this(context, attrs, 0, DEFAULT_SCALE);}
  public SuitView(Context context, AttributeSet attrs, int defStyle) {this(context, attrs, defStyle, DEFAULT_SCALE);}
  public SuitView(Context context, AttributeSet attrs, int defStyle, float scale)
  {
    super(context, attrs, defStyle);

    this.scale = scale;

    if (!isInEditMode())
      DeckManager.instance(context).register(this);
  }

  public void set(Card.Suit suit)
  {
    this.suit = suit;

    refresh();
  }

  @Override
  public void refresh()
  {
    setImageDrawable(suit == null ? null : DeckManager.instance(getContext()).image(suit, isEnabled()));
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    int min = Math.round(Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)) * scale);

    super.onMeasure(MeasureSpec.makeMeasureSpec(min, MeasureSpec.getMode(widthMeasureSpec)),
                    MeasureSpec.makeMeasureSpec(min, MeasureSpec.getMode(heightMeasureSpec)));
  }

  private static final float DEFAULT_SCALE = 1;

  private Card.Suit suit;
  private float scale;
}
